package com.authsphere.plugin.entity;

import com.authsphere.plugin.wrapper.ModifiableHttpServletRequest;
import jakarta.servlet.http.HttpServletRequest;

import java.util.function.Consumer;

/**
 * @program: AuthSphere
 * @description:
 * @author: YuKai Fan
 * @create: 2025/3/28 15:22
 **/
public class ParameterCryptFieldBean implements CryptFieldBean {

    private final String fieldValue;

    private final String fieldName;

    private final HttpServletRequest request;

    private final Consumer<HttpServletRequest> afterSetFieldValue;

    public ParameterCryptFieldBean(String fieldValue, String fieldName, HttpServletRequest request, Consumer<HttpServletRequest> afterSetFieldValue) {
        this.fieldValue = fieldValue;
        this.fieldName = fieldName;
        this.request = request;
        this.afterSetFieldValue = afterSetFieldValue;
    }

    @Override
    public String getFieldName() {
        return fieldName;
    }

    @Override
    public String getFieldValue() {
        return fieldValue;
    }


    public HttpServletRequest getRequest() {
        return request;
    }

    @Override
    public void setFieldValue(String newValue) {
        ModifiableHttpServletRequest modifiableHttpServletRequest = new ModifiableHttpServletRequest(this.getRequest());
        modifiableHttpServletRequest.addParameter(getFieldName(), newValue);
        afterSetFieldValue.accept(modifiableHttpServletRequest);
    }
}
